package com.ruiyian.common.utils;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.DeserializationFeature;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.SerializationFeature;
import org.apache.poi.ss.formula.functions.T;

import java.io.IOException;

//import lombok.Data;

/**
 * @author sairo
 * @version 1.0
 * @date 2020-09-22 14:51:51
 */
//@Data
public class JSON {


    public static ObjectMapper objectMapper = new ObjectMapper();


    static {
        // 转换为格式化的json
        objectMapper.enable(SerializationFeature.INDENT_OUTPUT);

        // 如果json中有新增的字段并且是实体类类中不存在的，不报错
        objectMapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
    }

    public static String stringify(Object object) {
        String str = "";
        try {
            str = objectMapper.writerWithDefaultPrettyPrinter().writeValueAsString(object);
        } catch (JsonProcessingException e) {
            e.printStackTrace();
        }
        return str;
    }

    public static T parse(String str, Class<T> clazz) {
        T ret = null;
        try {
            ret  = objectMapper.readValue(str, clazz);
        } catch (IOException e) {
            e.printStackTrace();
        }
        return ret;
    }
}
